package com.yqyd.web.controller.nbot.app;
import com.yqyd.common.constant.NbotConstans;
import com.yqyd.common.core.domain.R;
import com.yqyd.common.core.domain.entity.SysUser;
import com.yqyd.common.utils.OssUtil;
import com.yqyd.common.utils.SecurityUtils;
import com.yqyd.nbot.dto.EditUserInfoDto;
import com.yqyd.nbot.dto.OverSetDto;
import com.yqyd.nbot.service.INbotUserNumberService;
import com.yqyd.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/nbot/mine")
@CrossOrigin
@Slf4j
public class MineApi {

    @Resource
    private ISysUserService userService;

    @Resource
    private INbotUserNumberService userNumberService;

    @Resource
    private OssUtil ossUtil;

    @GetMapping("/Info")
    public R<Map<String, Object>> Info() {
        Map<String, Object> map = new HashMap<>();
        Long userId = SecurityUtils.getUserId();
        // 获取个人的信息
        map.put("Info", userService.selectEasyUserByUserId(userId));
        // 获取用户的数据
        map.put("Data", userNumberService.selectNbotUserNumberByUserId(userId));
        return R.ok(map);
    }

    @PostMapping("/OverSet")
    public R<String> OverSet(@RequestBody @Valid OverSetDto dto) {
        Long userId = SecurityUtils.getUserId();
        SysUser user = new SysUser();
        user.setUserId(userId);
        user.setPhonenumber(dto.getPhone());
        user.setNickName(dto.getNickName());
        user.setCity(dto.getCity());
        user.setSex(dto.getSex());
        user.setBirthDate(dto.getBirthday());
        user.setRealName(dto.getRealName());
        user.setIdCard(dto.getIdCard());
        // 认证成功
        user.setAuthStatus(NbotConstans.USER_AUTH_SUCCESS);
        userService.updateUser(user);
        return R.ok();
    }

    @PutMapping("/EditInfo")
    public R<String> EditInfo(@RequestBody @Valid EditUserInfoDto dto) {
        Long userId = SecurityUtils.getUserId();
        SysUser user = new SysUser();
        user.setUserId(userId);
        user.setAvatar(dto.getAvatar());
        user.setPhonenumber(dto.getPhonenumber());
        user.setNickName(dto.getNickName());
        user.setCity(dto.getCity());
        user.setSex(dto.getSex());
        user.setBirthDate(dto.getBirthDate());
        userService.updateUser(user);
        return R.ok();
    }

    @PostMapping("/UploadTempAvatar")
    public R<String> UploadTempAvatar(@RequestParam("file") MultipartFile file) {
        // todo 上传头像到OSS
        try {
            log.info("接收到文件上传请求: {}, 大小: {} bytes", file.getOriginalFilename(), file.getSize());
            // 验证文件
            if (file.isEmpty()) {
                return  R.fail("文件不能为空");
            }
            // 验证文件大小（2MB限制）
            if (file.getSize() > 2 * 1024 * 1024) {
                return R.fail("文件大小不能超过2MB");
            }
            // 上传文件
            return R.ok(ossUtil.uploadFile(file));

        } catch (IllegalArgumentException e) {
            log.warn("文件上传参数错误: {}", e.getMessage());
            return R.fail("参数错误: " + e.getMessage());
        } catch (Exception e) {
            log.error("文件上传失败: {}", e.getMessage(), e);
            return R.fail("文件上传失败: " + e.getMessage());
        }
    }

}
